import { createRouter, createWebHistory } from 'vue-router';
import { createRouterGuards } from './router-guards';
import { whiteNameList } from './constant';
import { isMobile } from '@/utils/device.js'

import DefaultLayout from '@/layouts/DefaultLayout.vue';
import MobileLayout from '@/mobileLayouts/index.vue';

const mobileRoutes = [
  {
    path: "/",
    component: MobileLayout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        name: "Dashboard",
        component: () => import('@/views/dashboard/index.vue'),
        meta: {
          title: "看板",
          showFooter: true
        }
      },
      {
        path: "workspace",
        name: "WorkspaceIndex",
        component: () => import('@/views/workspace/index.vue'),
        meta: {
          title: "工作台",
          showFooter: true
        }
      },
      {
        path: "workspace/:menuId",
        name: "WorkspaceDetail",
        component: () => import('@/views/workspace/index.vue'),
        meta: {
          title: "工作台",
          showFooter: false
        }
      },

      {
        path: "profile",
        name: "Profile",
        component: () => import('@/views/profile/index.vue'),
        meta: {
          title: "我的",
          showFooter: true
        }
      }
    ]
  }
]
// 路由组件
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
  },
  {
    path: '/redirect/:pathMatch(.*)*',
    name: 'RedirectWithPath',
    component: () => import('@/views/Redirect.vue'),
  },
  {
    path: '/monitoring',
    name: 'Monitoring',
    component: () => import('@/views/monitoring/index.vue'),
  }
];

if (isMobile) {
  routes.push(...mobileRoutes)
}

// 创建路由实例
const router = createRouter({
  history: createWebHistory(''),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  },
});

export async function setupRouter(app) {
  // 创建路由守卫
  createRouterGuards(router, whiteNameList);

  app.use(router);
  // 路由准备就绪后挂载APP实例
  await router.isReady();
}
export default router;
